import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/config/font.dart';
import 'package:development_assistant/controller/docker_controller.dart';
import 'package:development_assistant/ui/screen/widget/custom_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class EditDocker extends StatelessWidget {

  final DockerController dockerController=Get.put(DockerController());
  final TextEditingController fromText=TextEditingController();
  final TextEditingController nameText=TextEditingController();
  final TextEditingController tagText=TextEditingController();
  final TextEditingController cidText=TextEditingController();
  final TextEditingController fileText=TextEditingController();

  EditDocker({super.key});


  @override
  Widget build(BuildContext context) {

    fromText.text=dockerController.dockerFrom.value;
    nameText.text=dockerController.dockerName.value;
    tagText.text=dockerController.dockerTag.value;
    cidText.text=dockerController.dockerCid.value;
    fileText.text=dockerController.dockerFile.value;


    return Scaffold(
      appBar: _appBar(),
      bottomNavigationBar: _bottomBtn(),
      resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 20.w,horizontal: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("Docker镜像".tr,style: FontConfig.titleStyle,),
              _dockerField("镜像源".tr,dockerController.dockerFrom,fromText),
              _dockerField("镜像名".tr,dockerController.dockerName,nameText),
              _dockerField("镜像标签".tr,dockerController.dockerTag,tagText),
              _dockerField("镜像ID".tr,dockerController.dockerCid,cidText),
              Text("Dockerfile",style: FontConfig.titleStyle,),
              _dockerfile(fileText),
              SizedBox(height: 20.h,)
            ],
          ),
        ),
      ),
    );
  }


  _bottomBtn(){
    return Container(
        width: 350.w,
        height: 35.h,
        margin: EdgeInsets.symmetric(vertical: 10.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
              onPressed: () async {
                Get.defaultDialog(
                  title: "修改Docker镜像".tr,
                  middleText: "${'是否修改Docker镜像?'.tr} : ${dockerController.dockerName}",
                  confirm: TextButton(
                    style: TextButton.styleFrom(
                        backgroundColor: ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)
                        )
                    ),
                    onPressed: (){
                      dockerController.updateDocker();
                    },
                    child: Text("确定".tr),
                  ),
                  cancel:TextButton(
                    style: TextButton.styleFrom(
                        backgroundColor:ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)
                        )
                    ),
                    onPressed: (){
                      Get.back();
                    },
                    child: Text("取消".tr),
                  ),
                );
              },
              style: ElevatedButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)
                  )
              ),
              child:Text("修改Docker镜像".tr,style: TextStyle(fontWeight: FontWeight.bold),),
            ),
            ElevatedButton(
              onPressed: () {
                Get.defaultDialog(
                  title: "删除Docker镜像".tr,
                  middleText: "${"是否删除Docker镜像?".tr} : ${dockerController.dockerName}",
                  confirm: TextButton(
                    style: TextButton.styleFrom(
                        backgroundColor: ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)
                        )
                    ),
                    onPressed: (){
                      dockerController.delDocker();
                    },
                    child: Text("确定".tr),
                  ),
                  cancel:TextButton(
                    style: TextButton.styleFrom(
                        backgroundColor: ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)
                        )
                    ),
                    onPressed: (){
                      Get.back();
                    },
                    child: Text("取消".tr),
                  ),
                );
              },
              style: ElevatedButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)
                  )
              ),
              child: Text("删除Docker镜像".tr,style: TextStyle(fontWeight: FontWeight.bold),),
            ),
          ],
        )
    );
  }

  _dockerField(String hintText,RxString dbValue,TextEditingController textEditingController){
    return Container(
      width: 320.w,
      height: 50.h,
      margin: EdgeInsets.symmetric(vertical: 5.h),
      child: Center(
        child: TextField(
          maxLines: 1,
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          controller: textEditingController,
          onChanged: (e){
            dbValue.value=e;
          },
        ),
      ),
    );
  }

  _dockerfile(TextEditingController textEditingController){
    return Container(
        height: 200.h,
        margin: EdgeInsets.symmetric(vertical: 10.h),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            border: Border.all(color:Colors.grey,width: 1)
        ),
        child: TextField(
          maxLines: null,
          decoration: const InputDecoration(
              border: OutlineInputBorder(
                  borderSide: BorderSide.none
              )
          ),
          controller: textEditingController,
          onChanged: (e){
            dockerController.dockerFile.value=e;
          },
        )
    );
  }

  _appBar(){
    return AppBar(
      title: Text("编辑Docker镜像".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          Get.offNamed("/Home",arguments: {
            "selectIndex":0
          });
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }
}
